package com.android.lovingpet.home;

import android.content.Context;
import android.os.Bundle;

import com.android.lovingpet.Application;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.util.HttpUtil;
import com.android.lovingpet.util.URLStore;
import com.android.lovingpet.util.Utility;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MineModel {

    private static MineModel instance;
    private MineModel() {}
    public static MineModel getInstance() {
        if (instance == null) {
            synchronized (MineModel.class) {
                if (instance == null) {
                    instance = new MineModel();
                }
            }
        }
        return instance;
    }

    public void sign(final Back back) {
        if (isSign()) {
            back.onSuccess(null);
            return;
        }

        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SIGN_SIGN, new HashMap<String, String>(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.onFailure(HttpUtil.getCodeString(status));
                    } else {
                        sign();
                        back.onSuccess(null);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void isSign(int id, final Back back) {
        if (isSign()) {
            back.onSuccess(null);
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SIGN_TODAY, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status == 13) {
                        sign();
                        back.onSuccess(null);
                    } else if (status == 14) {
                        back.onFailure(HttpUtil.getCodeString(status));//只有已签到才返成功
                    } else {
                        back.onFailure(HttpUtil.getCodeString(status));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getSignDays(int id, final Back back) {
        if (isSign()) {
            Bundle bundle = new Bundle();
            bundle.putInt("days", getSignDays());
            back.onSuccess(bundle);
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SIGN_DAYS, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.onFailure(HttpUtil.getCodeString(status));
                    } else {
                        int days = object.getInt("day");
                        setSignDays(days);
                        Bundle bundle = new Bundle();
                        bundle.putInt("days", days);
                        back.onSuccess(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private synchronized void sign() {
        Application.getContext()
                .getSharedPreferences("sign", Context.MODE_PRIVATE)
                .edit()
                .putLong("time", System.currentTimeMillis())
                .apply();
    }

    public boolean isSign() {
        long now = System.currentTimeMillis();
        long before = Application.getContext()
                .getSharedPreferences("sign", Context.MODE_PRIVATE)
                .getLong("time", 0);
        return now - before < 24 * 60 * 60 * 1000;
    }

    private synchronized void setSignDays(int days) {
        Application.getContext()
                .getSharedPreferences("sign", Context.MODE_PRIVATE)
                .edit()
                .putInt("sign", days)
                .apply();
    }

    public int getSignDays() {
        return Application.getContext()
                .getSharedPreferences("sign", Context.MODE_PRIVATE)
                .getInt("sign", 0);
    }
}
